package com.splant.android.smart.UILevel.Community;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.splant.android.R;
import com.splant.android.smart.ModelLevel.WaterListBean;
import com.splant.android.smart.UILevel.Community.fragments.Garden_DetectorList_Adapter;
import com.splant.android.smart.UILevel.Utils.sPlantUitl;

import java.util.List;

/**
 * Created by Administrator on 2017/8/12.
 */

public class Garden_Water_Adapter extends RecyclerView.Adapter<Garden_Water_Adapter.ViewHolder> {

    private LayoutInflater mInflater;
    private List<WaterListBean.Water> waterList;
    private Context mContext;

    public Garden_Water_Adapter(Context context, List<WaterListBean.Water> waterList) {
        mInflater = LayoutInflater.from(context);
        mContext = context;
        this.waterList = waterList;
    }

    @Override
    public Garden_Water_Adapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

        View view = mInflater.inflate(R.layout.garden_water_adapter_item, viewGroup, false);

        ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.img_water = (ImageView) view.findViewById(R.id.img_water);
        viewHolder.tv_waterName = (TextView) view.findViewById(R.id.tv_waterName);
        viewHolder.ll_water = (LinearLayout) view.findViewById(R.id.ll_water);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(Garden_Water_Adapter.ViewHolder holder, final int position) {

        int deviceType = sPlantUitl.getWaterType(waterList.get(position).getWaterId());

        if (deviceType == 1)
            holder.img_water.setImageResource(R.mipmap.garden_ic_water1_online);
        else if (deviceType == 2)
            holder.img_water.setImageResource(R.mipmap.garden_ic_water2_online);
        else
            holder.img_water.setImageResource(R.mipmap.garden_ic_water3_online);

        holder.tv_waterName.setText(waterList.get(position).getWaterName());

        holder.ll_water.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mListener != null)
                    mListener.onItemChick(position);

                selectItemIndex = position;
            }
        });

        if (position == selectItemIndex)
            holder.ll_water.setBackgroundResource(R.color.garden_color0);
        else
            holder.ll_water.setBackgroundResource(R.color.garden_color1);
    }

    private int selectItemIndex = 0;

    @Override
    public int getItemCount() {
        return waterList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View arg0) {
            super(arg0);
        }

        ImageView img_water;
        TextView tv_waterName;
        LinearLayout ll_water;
    }

    private Garden_DetectorList_Adapter.onItemClickListener mListener = null;

    public void setOnItemClickListener(Garden_DetectorList_Adapter.onItemClickListener mListener) {
        this.mListener = mListener;
    }

    public interface onItemClickListener {
        void onItemChick(int pos);
    }
}
